{
     //ES3 ES5数据保护
    var Person = function(){
        var data = {
            name: 'es3',
            sex: 'male',
            age: 15
        }

        this.get = function(key){
            return data[key];
        }
        this.set = function(k,v){
            if(k !== 'sex'){
                data[k] = v;
            }
        }
    }
 
//声明一个实例
var person = new Person();
//读取
console.table({
    "name": person.get("name"),
    "sex": person.get("sex"),
    "age": person.get("age")
})
person.set("name", "es3-cname");
//读取
console.table({
    "name": person.get("name"),
    "sex": person.get("sex"),
    "age": person.get("age")
})
person.set("sex", "femal");
//读取
console.table({
    "name": person.get("name"),
    "sex": person.get("sex"),
    "age": person.get("age")
})

}



{
    //ES5 数据保护
    var Person = {
        name: 'es5',
        age: 15
    }

    Object.defineProperty(Person,'sex',{
        writable: false,
        value: 'male'
    })

    console.table({
        name: Person.name,
        age: Person.age,
        sex: Person.sex
    })

    Person.name='es5-cname';
    console.table({
        name: Person.name,
        age: Person.age,
        sex: Person.sex
    })
   try {
    Person.sex='female';
    console.table({
        name: Person.name,
        age: Person.age,
        sex: Person.sex
    })
   } catch (e) {
       console.log(e)
   }
}
///////////////////////////// ES6  设置保护的规则   /////////////////////////
{

    let Person = {
        name: 'es6',
        sex: 'male',
        age: 15
    };
    let person = new Proxy(Person, {
        get(target, key){
            return target[key];
        },
        set(target, k, v){
            if(k !== 'sex'){
                target[k] = v;
            }
        }
    })

    console.table({
        name: person.name,
        age: person.age,
        sex: person.sex
    })

    try {
        person.sex = 'female';
        console.table({
        name: person.name,
        age: person.age,
        sex: person.sex
    })
    } catch (e) {
        console.log(e)
    }
}